const fs = require('fs');
const path = require('path');
var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');

const gm = require('gm');

const imageToBase64 = require('image-to-base64');

module.exports = class extends controller {
    constructor() {
        super();
    }
    index(req, res) {
        res.render(req.TPL, {});
    }
    upload(req, res) {
        let p = req.body;
        /*
 {
  x: '480',
  y: '120',
  width: '960',
  height: '960',
  rotate: '0'
}
        */
        console.log(p);
        gm(tools.publicPath + 'static/cropper/example.jpg')
            .crop(p.width, p.height, p.x, p.y)
            .resize(200, 200)
            .write(tools.publicPath + 'static/cropper/example_200.jpg', function (err) {
                console.log(err);
            });
        res.json({
            code: 0,
            msg: 'ok',
        });
    }
    async base64(req, res) {
        let img = await imageToBase64(tools.publicPath + 'imgs/Seoul15.jpg');
        
        res.render(req.TPL, {
            img
        });
    }
}